package combat.protocol.data;

import java.io.Serializable;


public class DoublePoint implements Serializable {

	private static final long serialVersionUID = 1L;

	private double x = 0.0;
	private double y = 0.0;

	/**
	 * DoublePoint constructor with given X and Y
	 * @param x
	 * @param y
	 */
	public DoublePoint(double x, double y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * DoublePoint constructor to clone a given DoublePoint
	 * @param old The DoublePoint to clone
	 */
	public DoublePoint(DoublePoint old) {
		this(old.getX(), old.getY());
	}

	/**
	 * DoublePoint constructor with default (0,0) coordinates
	 */
	public DoublePoint() {}

	public void setX(double x) {
		this.x = x;
	}
	public double getX() {
		return x;
	}

	public double getY() {
		return y;
	}
	public void setY(double y) {
		this.y = y;
	}

	public void setXY(double x, double y) {
		this.x = x;
		this.y = y;
	}

	public String toString() {
		return "(" + this.x + ", " + this.y + ")";
	}

	/**
	 * Measures the distance between two DoublePoints
	 * @param yonder Other DoublePoint to get the distance to
	 * @return double The distance from this to yonder
	 */
	public double distanceTo(DoublePoint yonder) {
		double deltaX = Math.abs(this.x - yonder.x);
		double deltaY = Math.abs(this.y - yonder.y);
		return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
	}

	/**
	 * Measures if the distance between two DoublePoints is smaller than the
	 * given margin
	 * @param yonder The DoublePoint to calculate with
	 * @param margin The maximum distance from this to yonder
	 * @return Whether yonder is within margin distance
	 */
	public boolean nears(DoublePoint yonder, double margin) {
		return this.distanceTo(yonder) < margin;
	}

	/**
	 * Measures if the given DoublePoint is within default range
	 * @param yonder The DoublePoint to calculate with
	 * @return Whether yonder is within the default margin distance
	 */
	public boolean nears(DoublePoint yonder) {
		return this.nears(yonder, 10);
	}

	/**
	 * Adds x and y values to the current DoublePoint
	 * @param x
	 * @param y
	 * @return Newly placed DoublePoint
	 */
	public DoublePoint add(double x, double y) {
		return new DoublePoint(this.x + x, this.y + y);
	}

	/**
	 * Adds a DoublePoint's x and y values to the current DoublePoint
	 * @param yonder The DoublePoint to extract the x and y values from
	 * @return Newly placed DoublePoint
	 */
	public DoublePoint add(DoublePoint yonder) {
		return this.add(yonder.x, yonder.y);
	}
}
